package com.studyspace.controller;

import com.studyspace.entity.User;
import com.studyspace.model.LoginModel;
import com.studyspace.model.RegisterModel;
import com.studyspace.result.Result;
import com.studyspace.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @Author liujilong
 * @Project study-space
 * @File LoginController
 * @Date 2021/11/26 10:43 下午
 **/
@Api(tags = "登录接口")
@RestController
@RequestMapping("/studyspace")
public class LoginController {

    @Autowired
    private UserService userService;

    @ApiOperation("欢迎")
    @ResponseBody
    @GetMapping
    public String index() {
        return "welcome to cd's study space";
    }

    @ApiOperation("注册账号")
    @PostMapping("/register")
    public Result register(@Valid @RequestBody RegisterModel registerModel, HttpServletRequest httpServletRequest) {
        User user = userService.register(registerModel, httpServletRequest);
        if (null == user) {
            return Result.errorMessage("该手机号已被注册");
        }

        userService.save(user);

        return Result.success();
    }
}
